package org.oschina.api.blog.catalog.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.blog.catalog.model.BlogCatalog;
import org.oschina.api.blog.catalog.service.BlogCatalogService;
import org.oschina.api.blog.catalog.vo.BlogCatalogVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/blog/catalog")
public class BlogCatalogController extends BaseController {

	@Autowired
	private BlogCatalogService catalogService;
	
	/**
	 * 返回当前用户的目录
	 * @return
	 */

	@RequestMapping(value="/token",method=RequestMethod.GET)
	public List<BlogCatalogVo> findByToken()
	{
		return catalogService.findByToken();
	}
	
	/**
	 * 根据用户返回
	 * @return
	 */
	@Validate
	@RequestMapping(value="/user",method=RequestMethod.GET)
	public List<BlogCatalogVo> findByUser
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return catalogService.findByUser(id);
	}
	
	/**
	 * 创建
	 * @param catalog
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="name",filter={TextOnlyFilter.class})
		}) BlogCatalog catalog
	)
	{
		return catalogService.create(catalog);
	}
	
	/**
	 * 更新
	 * @param catalog
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class})
		}) BlogCatalog catalog
	)
	{
		return catalogService.update(catalog);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param target
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer target
	)
	{
		return catalogService.delete(id, target);
	}
}